//
//  ST_SnapchatBrowserController.swift
//  btvideo
//
//  Created by mac on 2020/10/16.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import NIMSDK
import SDWebImage

class ST_SnapchatBrowserController: ST_BTLightBarViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .black
        
        own_navigation_bar.backgroundColor = .clear
        
        view.insertSubview(cover_image_view, belowSubview: own_navigation_bar)
        
        //        view.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            
            guard let message = dataMsg else { return  }
            
            var displayImageWidth = message.chatImageSize.width
            var displayImageHeight = message.chatImageSize.height
            
            if displayImageWidth > ScreenWidth{
                
                displayImageHeight = ScreenWidth/displayImageWidth*displayImageHeight
                
                displayImageWidth = ScreenWidth
                
            }else if displayImageHeight > ScreenHeight{
                
                displayImageWidth = ScreenHeight/displayImageHeight*displayImageWidth
                
                displayImageHeight = ScreenHeight
            }
            
            make.width.equalTo(displayImageWidth)
            make.height.equalTo(displayImageHeight)
            
        }
        
        //        view.addSubview(chat_picture_mask_view)
        view.insertSubview(chat_picture_mask_view, belowSubview: own_navigation_bar)
        
        chat_picture_mask_view.snp.makeConstraints { (make) in
            make.edges.equalTo(cover_image_view)
        }
        
        bind_data()
        
        view.addSubview(seconds_label)
        seconds_label.snp.makeConstraints { (make) in
            make.width.height.equalTo(TIANCompatibleValue(value: 36))
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-40-BTSafeAreaBottom)
        }
        
    }
    
    fileprivate func bind_data(){
        
        
        guard let message = dataMsg else { return  }
        
        //            判断 本地有没有这个图片
        if let localPath = message.imageLocalURL,FileManager.default.fileExists(atPath: localPath),let localImage = localPath.load_image_from_disk(){
            
            cover_image_view.image = localImage
            
        }else {
            
            cover_image_view.sd_setImage(with: message.imageRemoteUrl?.url, completed: nil)
        }
        
    }
    
    var dataMsg:ST_DTChatMessage?
    
    
    //    MARK: - Properties
    fileprivate lazy var chat_picture_mask_view:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.setBackgroundImage(UIImage.init(color: UIColor.init(hex: 0xFEC207, transparency: 0.9)!, size: CGSize.init(width: 1, height: 1)), for: UIControl.State.normal)
        
        control.setBackgroundImage(UIImage.init(color: UIColor.init(white: 0, alpha: 0.9), size: CGSize.init(width: 1, height: 1)), for: UIControl.State.selected)
        
        control.setImage(ST_ImageUtils.imageName("snapchat_long_icon"), for: UIControl.State.normal)
        
        control.setImage(ST_ImageUtils.imageName("snapchat_long_icon_close"), for: UIControl.State.selected)
        
        control.isUserInteractionEnabled = false
        
        return control
    }()
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        
        control.isUserInteractionEnabled  = true
        
        let longPressRecognizer = UILongPressGestureRecognizer.init(target: self, action: #selector(long_pressed(sender:)))
               
       control.addGestureRecognizer(longPressRecognizer)
        
        return control
    }()
    
    fileprivate lazy var secounds = 3
    
    fileprivate lazy var seconds_normal_color = UIColor.init(hex: 0xFF1F56)
    
    fileprivate lazy var seconds_disable_color = UIColor.init(hex: 0xACB4C0)
    
    fileprivate lazy var seconds_label:UILabel = {
        let control = UILabel.init()
        control.text = "3s"
        control.textColor = seconds_normal_color
        control.cornerRadius = TIANCompatibleValue(value:36)/2.0
        control.borderColor = seconds_normal_color
        control.borderWidth = 2
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: TIANCompatibleValue(value:14), weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate var timer:Timer?
    
    @objc func updateSeconds(){
        
        secounds -= 1
        seconds_label.text = "\(secounds)s"
        
        if secounds == 0{
            
            stop_preview()
        }
    }
    
    fileprivate func startPreview(){
        
        chat_picture_mask_view.isHidden = true
        
        timer = Timer.init(timeInterval: 1, target: self, selector: #selector(updateSeconds), userInfo: nil, repeats: true)
        
        if let t = timer{
            
            RunLoop.current.add(t, forMode: RunLoop.Mode.common)
        }
        
    }
    
    
    fileprivate func stop_preview(){
        
        seconds_label.textColor = seconds_disable_color
        seconds_label.borderColor = seconds_disable_color
        
        secounds = 0
        seconds_label.text = "\(secounds)s"
        
        chat_picture_mask_view.isHidden = false
        chat_picture_mask_view.isSelected = true
        
        cover_image_view.isUserInteractionEnabled = false
        
        timer?.invalidate()
        timer = nil
        
        
        guard let message = dataMsg else { return  }
        
        let session = NIMSession.init(message.chatID, type: NIMSessionType.P2P)
        
        ST_DTChatFeed.shared.update_local_ext(ext: ["isSnapDestroy":true], session: session, msgId: message.uuid)
        
    }
    
    @objc func long_pressed(sender: UILongPressGestureRecognizer)
    {
        
        print("阅后即焚 state -- ",sender.state.rawValue)
        
        if sender.state == .began {
          
            print("阅后即焚 开始啦-----")
            
            self.startPreview()
            
        }else if sender.state != .changed{
            
            print("阅后即焚 焚掉啦-----")
            
            self.stop_preview()
            
        }
        
        
//        sender.state == .
        
    }
}
